package com.tzl.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class MyWebConfig {
	//配置CORS跨域过滤器
	@Bean
	public CorsWebFilter corsWebFilter() {
		// 创建Cors配置源
		UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
		// 创建Cors配置，允许所有头、方法和来源
		CorsConfiguration config = new CorsConfiguration();
		config.addAllowedHeader("*");
		config.addAllowedMethod("*");
		config.addAllowedOrigin("*");
		config.setAllowCredentials(true);
		// 注册Cors配置
		configSource.registerCorsConfiguration("/**", config);
		// 返回CorsWebFilter
		return new CorsWebFilter(configSource);
	}
}
